package com.orangelife.mobile.main.biz;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.CBPageAdapter;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/3.
 */
public class NetworkImageHolderView implements CBPageAdapter.Holder<String> {
    private ImageView imageView;
    public static List<Map<String, Object>> listmap = null;
    private Context mContext = null;

    public static void setListMap(List<Map<String, Object>> list) {
        listmap = list;
    }

    private DisplayImageOptions options;

    @Override
    public View createView(Context context) {
        //你可以通过layout文件来创建，也可以像我一样用代码创建，不一定是Image，任何控件都可以进行翻页
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.image_xfermode, null);
        imageView = (ImageView) view.findViewById(R.id.image_xfermode);
        this.mContext = context;
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.default_banner)
                .showImageForEmptyUri(R.drawable.default_banner)
                .showImageOnFail(R.drawable.default_banner)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(6))
                .build();
        return view;
    }

    @Override
    public void UpdateUI(Context context, final int position, String data) {
        ImageLoader.getInstance().displayImage(data, imageView, options);
        // id.download(data,imageView,true,300,100,10, ImageDownloader.ImageSize.IMAGE_SIZE_MIDDLE_PNG);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String, Object> advert = listmap.get(position);
                String paramUrl = String.valueOf(advert.get("url"));
                String paramTitle = String.valueOf(advert.get("title"));
                jump(getURL(paramUrl, paramTitle),paramTitle);
            }
        });
    }

    public static String getURL(String paramUrl, String paramTitle) {
        if (paramUrl!=null&&!paramUrl.contains("title=")) {/*如果URL中已经带title参数,则JSON中获得的标题不加入尾部*/
            if (!StringUtil.isBlank(paramTitle)) {
                if (!paramUrl.contains("?")) {/*如果URL不带其他参数,即没有 "?" ,则添加 "?",否则添加"&"*/
                    paramUrl = paramUrl + "?title=" + paramTitle;
                } else {
                    paramUrl = paramUrl + "&title=" + paramTitle;
                }
            }
        }
        return paramUrl;
    }

    private void jump(String url,String title) {
        if (!StringUtil.isBlank(url)) {/*如果URL为空,则不做任何操作*/
            Map<String,Object> map=new HashMap<>();
            map.put("UI_ELMT","picBanner");
            map.put("UI_PATH","/houseKpr");
            map.put("bannerName",title);
            JumpUtil.jumpActivity(mContext, url,map);
        }
    }


}
